Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
function getControl("Subgrid").addOnLoad execute twice when parent record is first created

Hello I made a function that detect if i added or deleted a new row in my subgrid and execute some coce according to that

Normally it works fine in an existing parent record unless I create a new parent record, i press save, then i add a row in my subgrid and my function executes twice for some reason

does anyone know why my code execute twice only when i create a new parent record, the function is in the onLoad even

I took the principle code from here : (the dosomething function fire twice)

function onLoad() {

    var funtionName = "onLoad";

    try {

        setTimeout(function () {

            if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contact_subgrid") != null && Xrm.Page.getControl("contact_subgrid") != undefined) {

                _rowCount = Xrm.Page.getControl("subgrid").getGrid().getTotalRecordCount();

                Xrm.Page.getControl("subgrid").addOnLoad(onGridLoad);

            }

        }, 5000);

    } catch (e) {

        Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description));

    }

}

function onGridLoad() {

    var functionName = " onGridLoad ";

    var currentRowCount = null;

    try {

        setTimeout(function () {

            if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contact_subgrid") != null && Xrm.Page.getControl("contact_subgrid") != undefined) {

                currentRowCount = Xrm.Page.getControl("subgrid").getGrid().getTotalRecordCount();

                if (currentRowCount > _rowCount) {

                    dosomething();

                    _rowCount = currentRowCount;

                }

                else if (currentRowCount < _rowCount) {

                    dosomethingelse();

                    _rowCount = currentRowCount;

                }

            }

        }, 2000);

    } catch (e) {

        Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description));

    }

}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code uses the Xrm.Page object, which is deprecated on Dynamics 365 CE Online / Dataverse. You should use the new API; see Client API Reference - MS Docs.

Your function is called twice, because the onLoad function gets called twice by the system: first when the form is opened in "Create" mode, second when the data on the form is saved. After the data has been saved, the form page is not reloaded, but nevertheless the form's onLoad function is called again. This causes your script to attach a second onLoad handler.

Try this code. It is based on the modern script API. Make sure your form passes the form context to the onLoad function:

function onLoad(c) {
    const formContext = c.getFormContext();
    const subGrid = formContext.getControl("subgrid");
    const contactSubGrid = formContext.getControl("contact_subgrid");

    if (subGrid === null || contactSubGrid === null)
        return;

    try {
        setTimeout(() => {
            let rowCount = subGrid.getGrid().getTotalRecordCount();

            subGrid.addOnLoad(controlContext => {
                setTimeout(() => {
                    let currentRowCount = subGrid.getGrid().getTotalRecordCount();

                    try {
                        if (currentRowCount > rowCount) {
                            // do something
                        }
                        else if (currentRowCount < rowCount) {
                            // do something else
                        }

                        rowCount = currentRowCount;
                    }
                    catch (e) {
                        Xrm.Navigation.openErrorDialog({ message: e.message, errorCode: e.errorCode });
                    }
                }, 2000);
            });
        }, 5000);
    }
    catch (e) {
        Xrm.Navigation.openErrorDialog({ message: e.message, errorCode: e.errorCode });
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda